<template>
  <div class="login-container">
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="@/assets/images/logo.png" />
      </template>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[
          {
            required: true,
            message: '请填写手机号',
            trigger: 'onChange',
            pattern: /^1[3456789]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="[
          {
            required: true,
            message: '请填写密码',
            trigger: 'onChange',
            pattern: /^\d{6}$/,
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          block
          type="info"
          round
          :loading="loading"
          loading-text="登录中..."
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>

import { Notify } from 'vant'
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data () {
    return {
      formLogin: {
        mobile: '17307407130',
        code: '246810' // 默认只能用246810后台规定了
      },
      loading: false
    }
  },
  methods: {
    ...mapActions(['AloginAction']),
    async onSubmit () {
      try {
        this.loading = true
        // console.log(this.formLogin)
        await this.AloginAction({ tel: this.formLogin.mobile, pwd: this.formLogin.code })
        Notify({ type: 'success', message: '登陆成功!', duration: 1000 })
        this.$router.replace({
          path: this.$route.query.from || '/layout'
        })
      } catch (e) {
        Notify({ type: 'warning', message: '手机号或密码错误' })
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
</style>
